﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Text;
using System.Collections;
using Xiaov.SmartWeb.Model;

namespace Xiaov.SmartWeb.Application.TZGC
{
    public partial class XMLB : BasePage
    {
        private string _moduleid;
        public string ModuleID
        {
            get
            {
                if (Request.QueryString["mid"] != null)
                {
                    _moduleid = Request.QueryString["mid"].ToString();
                }
                else
                {
                    _moduleid = "0000";
                }
                return _moduleid;
            }
        }

        //权限检查
        protected void Page_Init(object sender, EventArgs e)
        {
            PM_Filter();
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                //形成树
                TreeView1.Nodes.Clear();
                IniTree(TreeView1.Nodes, "0");
                TextBox3.Enabled = false;
            }
        }
       

        #region 递归形成树形菜单的函数
        //建树的基本思路是：从根节点开始递归调用显示子树 
        private void IniTree(TreeNodeCollection Nds, string parentId)
        {
            //形成树形菜单用的DataTable、DataSet
            DataTable treedt;
            DataSet treeds;

            treedt = new DataTable();
            treeds = new DataSet();

            //查询节点的SQL
            string treesql = @"select * from yt_xmlbb order by sx  ";


            treeds = Maticsoft.DBUtility.DbHelperOra.Query(treesql);
            treedt = treeds.Tables[0];

            DataView treedv = new DataView();
            TreeNode tmpNd;
            string intId;
            treedv.Table = treeds.Tables[0];
            treedv.RowFilter = "FLBBM='" + parentId + "'";
            foreach (DataRowView drv in treedv)
            {
                tmpNd = new TreeNode();
                tmpNd.Value = drv["LBBM"].ToString();
                //tmpNd.Tag = drv["PID"].ToString();
                tmpNd.Text = drv["LBMC"].ToString();                
                Nds.Add(tmpNd);
                intId = drv["FLBBM"].ToString();
                //InitTree(tmpNd.Nodes, tmpNd.Tag.ToString());
                IniTree(tmpNd.ChildNodes, tmpNd.Value);
            }
        }

       
        #endregion

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            //绑定所选节点信息
            string lbbm = TreeView1.SelectedNode.Value;
            string lbmc,flbbm;
            lbmc = null;
            flbbm = null;
            string sql;
            sql = "select lbmc,flbbm from yt_xmlbb where lbbm = '" + lbbm + "'";
            OracleDataReader mydr;
            mydr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(sql);
            while (mydr.Read())
            {
                lbmc = mydr["lbmc"].ToString();
                flbbm = mydr["flbbm"].ToString();                
            }
            
            TextBox1.Enabled = false;
            TextBox2.Enabled = true;

            //实际用的
            TextBox1.Text = lbbm;
            TextBox2.Text = lbmc;
            TextBox3.Text = flbbm;

            //增加节点用的
            TextBox4.Text = lbbm;
            TextBox5.Text = flbbm;
        }

        //保存节点
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            string sql_save;

            if (TextBox1.Enabled == false)  //修改
            {
                sql_save = "update yt_xmlbb set lbmc = '" + TextBox2.Text.ToString().Trim() + "',flbbm = '" + TextBox3.Text.ToString().Trim() + "' where lbbm = '" + TextBox1.Text.ToString().Trim() + "'";
                Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql_save);
            }
            else   //新增
            {
                string sql_query;
                int flag;
                sql_query = "select count(*) from yt_xmlbb where lbbm = '" + TextBox1.Text.ToString().Trim() + "'";
                flag = Convert.ToInt32(Maticsoft.DBUtility.DbHelperOra.GetSingle(sql_query));
                if (flag == 0)
                {

                    sql_save = "insert into yt_xmlbb (lbbm,lbmc,flbbm) values ('" + TextBox1.Text.ToString().Trim() + "','" + TextBox2.Text.ToString().Trim() + "','" + TextBox3.Text.ToString().Trim() + "')";
                    Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql_save);
                }
                else
                {
                    Response.Write("<script>alert('类别编码重复!')</script>");
                }
            }
            //刷新树
            TreeView1.Nodes.Clear();
            IniTree(TreeView1.Nodes, "0");
        }

        //新增同级节点
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            //清空
            TextBox1.Text = null;
            TextBox2.Text = null;
            TextBox3.Text = null;

            TextBox1.Enabled = true;
            TextBox2.Enabled = true;

            TextBox1.Text = "_" + TextBox5.Text.ToString().Trim();
            TextBox3.Text = TextBox5.Text.ToString().Trim();
        }

        //增加子级节点
        protected void btn_AddChild_Click(object sender, EventArgs e)
        {
            //清空
            TextBox1.Text = null;
            TextBox2.Text = null;
            TextBox3.Text = null;

            TextBox1.Enabled = true;
            TextBox2.Enabled = true;

            TextBox1.Text = "_" + TextBox4.Text.ToString().Trim();
            TextBox3.Text = TextBox4.Text.ToString().Trim();


        }

        //删除某个节点（必须是末级节点）
        protected void btn_Del_Click(object sender, EventArgs e)
        {
            if (TreeView1.SelectedValue != null && TreeView1.SelectedValue.ToString() != "")
            {
                int flag = 1;
                string current_lbbm; //当前节点的value，即类别编码
                current_lbbm = TreeView1.SelectedValue.ToString();
                string sql_query = "select count(*) from yt_xmlbb where flbbm = '" + current_lbbm + "'";
                flag = Convert.ToInt32(Maticsoft.DBUtility.DbHelperOra.GetSingle(sql_query));
                if (flag == 0)
                {
                    string sql_del;
                    sql_del = "delete from yt_xmlbb where lbbm = '" + current_lbbm + "'";
                    Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql_del);
                    Response.Write("<script>alert('删除成功!')</script>");

                    //刷新树
                    TreeView1.Nodes.Clear();
                    IniTree(TreeView1.Nodes, "0");

                    //清空
                    TextBox1.Text = null;
                    TextBox2.Text = null;
                    TextBox3.Text = null;
                    TextBox4.Text = null;
                    TextBox5.Text = null;

                    TextBox1.Enabled = true;
                    TextBox2.Enabled = true;
                }
                else
                {
                    Response.Write("<script>alert('只能删除末级节点!')</script>");
                }
            }
        }

       

        

        
    }
}